<?php


function getJpGraphConfig($config = null) {
	$jpGraph_config['width']  = 650;
	$jpGraph_config['height'] = 300;
	$jpGraph_config['colors'] = array('#588E03','#F9630D','#FFEC00','#9CCE2E');
	$jpGraph_config['margin'] = array(40,20,40,20);
	$jpGraph_config['color'] = $jpGraph_config['colors'][0];
	$jpGraph_config['xTitle'] = "X-title";
	$jpGraph_config['yTitle'] = "Y-title";
	$jpGraph_config['legend']  = array();
	
	if (is_array($config))
		return array_merge($jpGraph_config, $config);
	else 
 		return $jpGraph_config;
}

function linechart($name, $ydata, $config = array()) {
	$config = getJpGraphConfig($config);

	$loc = getChartFileLocation($name);
	if (file_exists($loc)) {
		return $name;
	}

    require_once("jpgraph/jpgraph.php");
    require_once("jpgraph/jpgraph_line.php");    

    // Create the graph. These two calls are always required
    $graph = new Graph($config['width'],$config['height'],"auto");
    $graph->SetScale("textlin");
	$graph->SetFrame(false);
	$graph->img->SetMargin($config['margin'][0],$config['margin'][1],$config['margin'][2],$config['margin'][3]);
    $graph->img->SetAntiAliasing();

    // Create the linear plot
    $lineplot=new LinePlot($ydata);
    $lineplot->SetColor($config['color']);
    
    // Add the plot to the graph
    $graph->Add($lineplot);

	$graph->Stroke($loc);
	return $name;
}

function piechart($name, $data, $legends, $config = null) {
	$config = getJpGraphConfig($config);
	
	$loc = getChartFileLocation($name);
	/*if (file_exists($loc)) {
		return $name;
	}*/

	require_once("jpgraph/jpgraph.php");
	require_once("jpgraph/jpgraph_pie.php");

	$graph = new PieGraph($config['width'],$config['height'],"auto");
    $graph->SetScale("textlin");
	$graph->SetFrame(false);
	$graph->img->SetMargin($config['margin'][0],$config['margin'][1],$config['margin'][2],$config['margin'][3]);
    $graph->img->SetAntiAliasing();


	// Create
	$p1 = new PiePlot($data);
	$p1->value->show=true;
	$p1->SetLegends($legends);
	$p1->SetSliceColors($config['colors']); 
	
	$graph->legend->Pos(0.5, 0, 'center'); 
	$graph->legend->SetLayout(LEGEND_HOR);
	
	$graph->Add($p1);
	$graph->Stroke($loc);
	
	return $name;
}

function barchart($name, $data, $xlabel = null, $config, $data2 = null) {
	$config = getJpGraphConfig($config);

	$loc = getChartFileLocation($name);
	
	/*if (file_exists($loc)) {
		return $name;
	}*/

	require_once("jpgraph/jpgraph.php");
	require_once("jpgraph/jpgraph_bar.php");

	$graph = new Graph($config['width'],$config['height'],"auto");    
    $graph->SetScale("textlin");
	$graph->SetFrame(false);
	$graph->img->SetMargin($config['margin'][0],$config['margin'][1],$config['margin'][2],$config['margin'][3]);
    $graph->img->SetAntiAliasing();



	$bplot = new BarPlot($data);
	$bplot->SetFillColor($config['color']);
	$bplot->value->SetFormat('%01.00f');
	$bplot->value->Show();
    	
	if ($data2 != null) {
		$bplot2 = new BarPlot($data2);
		$bplot2->SetFillColor($config['colors'][1]);
		$bplot2->value->Show();

		$bplot1 = $bplot;
		$bplot  = new GroupBarPlot(array($bplot1,$bplot2));
	}

	$graph->Add($bplot);
	
	if ($xlabel != null) 
		$graph->xaxis->SetTickLabels($xlabel);
		
	
	$graph->xaxis->title->Set($config['xTitle']);
	$graph->yaxis->title->Set($config['yTitle']);
	
	$graph->Stroke($loc);
	return $name;
}

function getChartFileLocation($name) {
	$n = $name."_".sha1($name).".png";
	$loc = "./system/cache/".$n;
	return $loc; 
}

?>